﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LiteUart
{
    public partial class Component1 : Panel
    {
        VScrollBar vb = new VScrollBar();
        int vbValue = 0;
        SolidBrush b1;
        Font font;

        SizeF mSizeF;
        string[] text = new string[1024];
        StringFormat sf;

        int lines = 100;
        int startLine = 0;
        int maxLines = 0;
        Graphics g;

        public Component1()
        {
            InitializeComponent();
        }

        public Component1(IContainer container)
        {
            container.Add(this);

            InitializeComponent();

            sf = StringFormat.GenericTypographic;
            sf.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;
            g = CreateGraphics();

            //单色填充
            b1 = new SolidBrush(Color.White);//定义单色画刷          
            //g.FillRectangle(b1, rect);//填充这个矩形
            font = new Font("微软雅黑", 12, FontStyle.Regular, GraphicsUnit.Point);

            mSizeF = g.MeasureString("Test", font, new PointF(0, 0), sf);
            maxLines = this.Width / (int)mSizeF.Height;


            vb.Parent = this;
            vb.Dock = DockStyle.Right;
            vb.Value = 0;
            vb.Maximum = lines;

            vb.Scroll += new ScrollEventHandler(this.Vb_Scroll);
            //初始化数据

            for (int i = 0; i < lines; i++)
            {
                text[i] = "测试数据测试数据测试数据测试数据测试" + i;
            }

        }

        private void Vb_Scroll(object sender, ScrollEventArgs e)
        {


            startLine = e.NewValue;

            // Console.WriteLine("重绘");
            // g.FillRectangle(new SolidBrush(Color.Red), rectangle);
            //字符串
            Invalidate();

            //Console.WriteLine(e.NewValue);
        }
        protected void Paint(object sender, PaintEventArgs e)
        {
            

            Graphics g = e.Graphics;
            g.Clear(Color.Black);
            // Console.WriteLine("重绘");
            // g.FillRectangle(new SolidBrush(Color.Red), rectangle);
            //字符串

            for (int i = 0; i < maxLines; i++)
            {
                g.DrawString(text[i + startLine], font, b1, 0, mSizeF.Height * i, sf); ;
            }

        }
    }
}
